{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "import warnings\n",
    "warnings.filterwarnings(\"ignore\")\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from contextlib import redirect_stdout\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.multiclass import OneVsRestClassifier\n",
    "from sklearn.svm import LinearSVC\n",
    "\n",
    "\n",
    "def get_data():\n",
    "    data = pd.read_csv(\"/kaggle/input/train-cnn/p2pData.txt\", sep=\" \", header=None)\n",
    "    label = pd.read_csv(\"/kaggle/input/train-cnn/p2pLabel.txt\", sep=\" \", header=None)\n",
    "    data_columns = data.shape[1]\n",
    "\n",
    "    for i in range(0, data_columns):\n",
    "        if data[i][0] != data[i][0]:\n",
    "            del data[i]\n",
    "\n",
    "    data_columns = data.shape[1]\n",
    "    data.columns = np.arange(0, data_columns)\n",
    "\n",
    "    return data, label\n",
    "\n",
    "\n",
    "data, label = get_data()\n",
    "\n",
    "scaler = StandardScaler()\n",
    "data = scaler.fit_transform(data)\n",
    "\n",
    "data = np.array(data)\n",
    "label = np.array(label)\n",
    "\n",
    "X_train, X_test, y_train, y_test = train_test_split(data, label, test_size=0.2)\n",
    "n_samples, n_features = X_train.shape\n",
    "\n",
    "svm_one_vs_all_clf = OneVsRestClassifier(\n",
    "    LinearSVC(C=1.0, loss='squared_hinge', random_state=0, max_iter=10000))\n",
    "svm_one_vs_all_clf.fit(X_train,y_train)\n",
    "with open('output.txt', 'w') as f:\n",
    "    with redirect_stdout(f):\n",
    "        print(\"score on training_set: \", svm_one_vs_all_clf.score(X_train, y_train))\n",
    "        print(\"score on test_set:\", svm_one_vs_all_clf.score(X_test, y_test))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
